﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SchoolManagerMVC.BusinessAbstract.Interfaces;
using SchoolManagerMVC.Repository.EntityFramework;
using SchoolManagerMVC.Web.Areas.Courses.ViewModels;

namespace SchoolManagerMVC.Web.Areas.Courses.Controllers
{
    public class HomeController : Controller
    {
        /// <summary>
        /// Course Area
        /// </summary>
        private IRepository _repo;
        public HomeController(IRepository repo)
        {
            _repo = repo;
            if (_repo == null)
                throw new NullReferenceException();
        }

        /*
        public ActionResult Index()
        {
            List<CourseViewModel> listViewModel = new List<CourseViewModel>();
            var courses = _repo.GetAll<Course>();            

            if (courses != null)
            {
                foreach (var curr in courses)
                {
                    listViewModel.Add(courseToVM(curr));
                }
            }

            return View(listViewModel);
        }

        public ActionResult Create()
        {
            addDropDowns();
            return View();
        }

        [HttpPost]
        public ActionResult Create(Course course)
        {

            if (!ModelState.IsValid)
            {
                addDropDowns();
                return View("Create", course);
            }

            _repo.Add<Course>(course);
            return RedirectToAction("Index");
        }

        public ActionResult Details(int? id)
        {
            if (id == null)
                return RedirectToAction("Index");

            Course result = _repo.GetSingle<Course>(e => e.CourseID == id) as Course;
            if(result !=null)
                return View(courseToVM(result));



            return RedirectToAction("Index");
        }

        public ActionResult Edit(int? id)
        {
            if (id == null)
                return RedirectToAction("Index");

            var course = _repo.GetSingle<Course>(e => e.CourseID == id) as Course;
            if(course == null)
                return RedirectToAction("Index");

            addDropDowns();
            return View(course);

        }

        [HttpPost]
        public ActionResult Edit(Course course)
        {
            if (!ModelState.IsValid)
            {
                addDropDowns();
                return View("Edit", course);
            }
            try
            {
                var result = _repo.GetSingle<Course>(e => e.CourseID == course.CourseID) as Course;
                result.CourseName = course.CourseName;
                result.CourseDescription = course.CourseDescription;
                result.CourseNumber = course.CourseNumber;
                result.Department = course.Department;
                result.Teacher = course.Teacher;

                _repo.Update<Course>(result);
                ViewBag.Message = "Record Saved";
            }
            catch
            {
                ViewBag.Message = "There was an error saving";
            }

            addDropDowns();
            return View("Edit", course);
            
        }

        public ActionResult Delete(int? id)
        {
            return View("Delete", _repo.GetSingle<Course>(e => e.CourseID == id));
        }

        [HttpPost]
        public ActionResult Delete(Course course)
        {
            var message = _repo.Remove<Course>((_repo.GetSingle<Course>(e => e.CourseID == course.CourseID) as Course));
            if(message.result)
                return RedirectToAction("Index");

            ViewBag.Message = message.Message;
                return View(_repo.GetSingle<Course>(e => e.CourseID == course.CourseID));
        }

        private void addDropDowns()
        {
            ViewBag.CourseTeacher = _repo.GetAll<FacultyMember>();
            ViewBag.CourseDepartment = _repo.GetAll<Department>();
        }

        private CourseViewModel courseToVM(Course passedCourse)
        {
            try
            {
                var teacherName = _repo.GetWhere<FacultyMember>(e => e.FacultyID == passedCourse.Teacher).FirstOrDefault().FirstLast;
                var departmentName = _repo.GetWhere<Department>(e => e.DepartmentID == passedCourse.Department).FirstOrDefault().Name;
                var studentCount = _repo.GetWhere<StudentInCourse>(e => e.CourseID == passedCourse.CourseID) == null ? 0 : _repo.GetWhere<StudentInCourse>(e => e.CourseID == passedCourse.CourseID).Count;                

                return (new CourseViewModel()
                {
                    course = passedCourse,
                    TeacherName = teacherName,
                    DepartmentName = departmentName,
                    NumberOfStudents = studentCount
                });
            }
            catch 
            {
                return (new CourseViewModel());
            }
        } */
    }
}
